package com.adnaan.backend.controller.admin;

import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.Dishes;
import com.adnaan.backend.entity.DishesSteps;
import com.adnaan.backend.entity.vo.DosageVo;
import com.adnaan.backend.service.DishesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * author: adnaan
 * create: 2025-02-19 13:13
 * Description:
 */
@Tag(name = "药膳管理")
@RestControllerMapping("/dishes")
public class DishesController {

    @Resource
    private DishesService dishesService;

    @PostMapping("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "药膳分页查询")
    public Result<Page<Dishes>> pageQuery(@RequestBody PageParams<Dishes> pageParams) {
        // 1.查询参数
        Dishes params = pageParams.getParams();
        LambdaQueryWrapper<Dishes> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getName() != null, Dishes::getName, params.getName());
        // 3.分页查询
        Page<Dishes> page = dishesService.page(pageParams.getPage(), lqw);
        page.getRecords().forEach(item -> {
            item.setDosage(dishesService.getDosage(item.getId()));
            item.setSteps(dishesService.getSteps(item.getId()));
        });
        return Result.success(page);
    }

    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增药膳数据")
    public Result<Boolean> insert(@RequestBody Dishes dishes) {
        if (dishesService.save(dishes)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新药膳数据")
    public Result<Boolean> update(@RequestBody Dishes dishes) {
        if (dishesService.updateById(dishes)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除药膳数据")
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (dishesService.removeById(id)) {

            return Result.success();
        }
        return Result.error();
    }

    /**
     * 添加原料
     */
    @PostMapping("/addMaterial")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "添加药膳原料")
    public Result<Boolean> addMaterial(@RequestBody DosageVo dosages) {
        if (dishesService.addMaterial(dosages)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 删除原料
     */
    @GetMapping("/deleteMaterial/{dishId}/{materialId}")
    @ApiOperationSupport(order = 6)
    @Operation(summary = "删除药膳原料")
    public Result<Boolean> deleteMaterial(@PathVariable Long dishId, @PathVariable Long materialId) {
        if (dishesService.deleteMaterial(dishId, materialId)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 添加编辑药膳制作步骤
     */
    @PostMapping("/addSteps")
    @ApiOperationSupport(order = 7)
    @Operation(summary = "添加药膳制作步骤")
    public Result<Boolean> addSteps(@RequestBody DishesSteps dishesSteps) {
        if (dishesService.saveSteps(dishesSteps)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 药膳步骤排序
     */
    @PostMapping("/sortSteps")
    @ApiOperationSupport(order = 8)
    @Operation(summary = "药膳步骤排序")
    public Result<Boolean> sortSteps(@RequestBody List<DishesSteps> dishesSteps) {
        if (dishesService.sortDishSteps(dishesSteps)) {
            return Result.success();
        }
        return Result.error();
    }
}
